package vn.team.clipxonxao.ui;

import vn.team.clipxonxao.model.CategoryList;
import vn.team.clipxonxao.request.CategoryRequest;
import vn.team.clipxonxao.respone.CategoryRespone;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.Loader;
import android.widget.Toast;

import com.mk.fw.connection.APIResponse;
import com.mk.fw.connection.ResponseReceiver;
import com.mk.fw.ui.MKFragmentActivity;

public class SplashScreen extends MKFragmentActivity implements ResponseReceiver {

	private CategoryList categoryItems;
	private final int LOADER_CATEGORY = 101;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_loading);
		restartRequestServer(LOADER_CATEGORY, new CategoryRequest(), this);
	}
	
	@Override
	public void onBaseLoaderReset(Loader<APIResponse> loader) {
		
	}

	@Override
	public void receiveResponse(Loader<APIResponse> loader, APIResponse response) {
		if(response!= null){
			CategoryRespone categoryResponse = (CategoryRespone) response;
			handleCategoryResponse(categoryResponse);
		}else{
			Toast.makeText(getApplicationContext(), getResources().getString(R.string.noData), Toast.LENGTH_LONG).show();
		}
	}

	private void handleCategoryResponse(CategoryRespone categoryResponse) {
		categoryItems = categoryResponse.getCategoryList();
		Intent myIntent = new Intent(getApplicationContext(), AppMain.class);
		Bundle bundle = new Bundle();
		bundle.putSerializable("listCategory", categoryItems);
		myIntent.putExtra("dataCategory", bundle);
		this.finish();
		startActivity(myIntent);
	}
	
}
